#include "game1.h"
#include "ui_game1.h"
#include "menu.h"
#include <QPainter>
#include <QDebug>
#include <QTimer>
#include <QMouseEvent>

map2 map22;
QTimer *timer2=new QTimer();
int speed2=500;
int airship=1;

Game1::Game1(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Game1)
{
    if (playthrough>=4)
    {
        //设置音乐
        QMediaPlaylist *musicList = new QMediaPlaylist(this);
        musicList->addMedia(QUrl("qrc:/resourse/music6.mp3"));

        QMediaPlayer *soundPlayer = new QMediaPlayer(this);
        soundPlayer->setPlaylist(musicList);
        soundPlayer->play();
        musicList->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
    }
    ui->setupUi(this);
    connect(timer2,&QTimer::timeout,this,&Game1::timerUpdate);
    timer2->stop();
    this->setAttribute(Qt::WA_DeleteOnClose);
}

Game1::~Game1()
{
    delete ui;
}

void Game1::paintEvent(QPaintEvent *)
{
    if(playthrough>=4&&airship==1)
    {
        if (resolution<=4)
        {
            resolution=5;
        }
        if (resolution>=5)
        {
            timer2->start(250);
        }
        map22.mapclear();
        airship--;
    }
    QPainter painter(this);

    //设置网格颜色
    QPen pen(Qt::gray);
    painter.setPen(pen);

    //以label左上角为原点
    painter.translate(ui->label->x(),ui->label->y());

    //清空画布
    QBrush brush(Qt::white,Qt::SolidPattern);
    painter.setBrush(brush);
    painter.drawRect(0,0,ui->label->width()-1,ui->label->height()-1);

    //绘制网格
    for(int x=0;x<=map22.getwidthnum();x++)
    {
        painter.drawLine(x*map22.getwidth(),0,x*map22.getwidth(),map22.getheightnum()*map22.getheight());
    }
    for(int y=0;y<=map22.getheightnum();y++)
    {
        painter.drawLine(0,y*map22.getheight(),map22.getwidthnum()*map22.getwidth(),y*map22.getheight());
    }

    //绘制细胞
    if (playthrough<=3)
    {
        brush.setColor(Qt::black);
        painter.setBrush(brush);
        for(int x=0;x<map22.getwidthnum();x++)
        {
            for(int y=0;y<map22.getheightnum();y++)
            {
                if(map22.getmap(x,y)==1)
                        painter.drawRect(x*map22.getwidth(),y*map22.getheight(),map22.getwidth(),map22.getheight());
            }
        }
        painter.end();
    }
    else
    {
        brush.setColor(Qt::black);
        painter.setBrush(brush);
        for(int x=0;x<map22.getwidthnum()+96;x++)
        {
            for(int y=0;y<map22.getheightnum();y++)
            {
                if(map22.getmap(x,y)==1)
                        painter.drawRect(x*map22.getwidth(),y*map22.getheight(),map22.getwidth(),map22.getheight());
            }
        }
        painter.end();
    }

    if (playthrough>=4)
    {
        int t=map22.check();
        //t=1;//test
        if (t==1)
        {
            if (airship==0)
            {
                airship++;
            }
            playthrough=5;
            timer2->stop();
            map22.mapclear();
            this->close();
            trueending *t=new trueending();
            t->show();
        }
    }
}

void Game1::mousePressEvent(QMouseEvent *event)
{
    if((event->button()==Qt::LeftButton)&&playthrough<=3)
    {
        QPoint point=event->pos()-ui->label->pos();
        //offset=event->pos()-ui->label->pos();
        if(point.x()>=0 and point.x()<=map22.getwidth()*map22.getwidthnum() and point.y()>=0 and point.y()<=map22.getheight()*map22.getheightnum())//防止超出范围
        {
            lastcellx=point.x()/map22.getwidth();
            lastcelly=point.y()/map22.getheight();
            map22.changemap(point.x()/map22.getwidth(),point.y()/map22.getheight());//改变细胞状态
            update();//重绘界面
        }
    }
}

void Game1::mouseMoveEvent(QMouseEvent *event)
{
    if((event->buttons() & Qt::LeftButton)&&playthrough<=3)
    {
        QPoint point=event->pos()-ui->label->pos();
        //offset=event->pos()-ui->label->pos();
        if(point.x()>=0 and point.x()<=map22.getwidth()*map22.getwidthnum() and point.y()>=0 and point.y()<=map22.getheight()*map22.getheightnum())//防止超出map范围
        {
            if(lastcellx!=point.x()/map22.getwidth() or lastcelly!=point.y()/map22.getheight())
            {
                lastcellx=point.x()/map22.getwidth();
                lastcelly=point.y()/map22.getheight();
                map22.changemap(point.x()/map22.getwidth(),point.y()/map22.getheight());
                update();//重绘界面
            }
        }
    }
}

void Game1::timerUpdate()
{
    map22.mapupdate();
    update();//更新界面
}

void Game1::on_startpushButton_clicked()
{
    if (playthrough<=3)
    {
    timer2->start(speed2);
    }
}

void Game1::on_pausepushButton_clicked()
{
    if (playthrough<=3)
    {
    timer2->stop();
    }
}

void Game1::on_clearpushButton_clicked()
{
    if (playthrough<=3)
    {
        timer2->stop();
        map22.mapclear();
        update();
    }
}

void Game1::on_backpushButton_clicked()
{
    if (airship==0)
    {
        airship++;
    }
    timer2->stop();
    map22.mapclear();
    this->close();
    study *s =new study();
    s->show();
}

void map2::mapupdate()
{
    int dx[8]={0,0,-1,1,-1,1,-1,1};
    int dy[8]={1,-1,0,0,1,1,-1,-1};

    memset(neighbor,0,sizeof(neighbor));//细胞清零

    //统计邻居细胞数量
    if (playthrough<=3)
    {
        for(int x=0;x<widthnum;x++)
        {
            for(int y=0;y<heightnum;y++)
            {
                if(map[x][y]==1)
                {
                    for(int i=0;i<8;i++)
                    {
                        if(dx[i]+x>=0 and dx[i]+x<=widthnum and dy[i]+y>=0 and dy[i]+y<=heightnum) //防止越界
                        {
                            neighbor[dx[i]+x][dy[i]+y]++;//
                        }
                    }
                }
            }
        }

        //改变细胞状态
        for(int x=0;x<widthnum;x++)
        {
            for(int y=0;y<heightnum;y++)
            {
                if(neighbor[x][y]==3)
                    map[x][y]=1;
                else if(neighbor[x][y]!=2)
                    map[x][y]=0;
            }
        }
    }
    else
    {
        for(int x=0;x<widthnum+96;x++)
        {
            for(int y=0;y<heightnum;y++)
            {
                if(map[x][y]==1)
                {
                    for(int i=0;i<8;i++)
                    {
                        if(dx[i]+x>=0 and dx[i]+x<=widthnum+96 and dy[i]+y>=0 and dy[i]+y<=heightnum) //防止越界
                        {
                            neighbor[dx[i]+x][dy[i]+y]++;//
                        }
                    }
                }
            }
        }

        //改变细胞状态
        for(int x=0;x<widthnum+96;x++)
        {
            for(int y=0;y<heightnum;y++)
            {
                if(neighbor[x][y]==3)
                    map[x][y]=1;
                else if(neighbor[x][y]!=2)
                    map[x][y]=0;
            }
        }
    }
}

void map2::mapclear()
{
    for(int x=0;x<widthnum+96;x++)
    {
        for(int y=0;y<heightnum;y++)
        {
            map[x][y]=0;
        }
    }
    if (playthrough>=4)
    {
        int airshipx[31]={0,0,1,1,3,3,3,3,4,4,4,4,5,5,7,7,8,8,8,8,9,9,10,10,11,11,11,11};
        int airshipy[31]={4,5,4,5,3,4,5,6,2,3,6,7,1,8,1,8,1,3,6,8,4,5,4,5,2,3,6,7};
        int gatex[21]={1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3};
        int gatey[21]={5,7,10,12,1,2,4,7,10,13,15,16,5,7,10,12};
        int star1x[51]={1,1,1,1,1,1,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,6,6,8,8,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,13,13,13,13,13,13};
        int star1y[51]={3,4,5,9,10,11,1,6,8,13,1,6,8,13,1,6,8,13,3,4,5,9,10,11,3,4,5,9,10,11,1,6,8,13,1,6,8,13,1,6,8,13,3,4,5,9,10,11};
        int fly2x[11]={1,1,2,3,3,4,4,4,4};
        int fly2y[11]={1,4,5,1,5,2,3,4,5};
        int fly6x[11]={1,1,2,3,3,4,4,4,4};
        int fly6y[11]={2,5,1,1,5,1,2,3,4};

        for (int i=0;i<28;i++)
        {
            map[airshipx[i]+10+resolution][airshipy[i]+19]=1;
        }
        for (int i=0;i<16;i++)
        {
            map[gatex[i]+20][gatey[i]-1]=1;
            map[gatex[i]+20][gatey[i]+31]=1;
            map[gatex[i]+30][gatey[i]]=1;
            map[gatex[i]+30][gatey[i]+30]=1;
            map[gatex[i]+40][gatey[i]+1]=1;
            map[gatex[i]+40][gatey[i]+29]=1;
        }
        for (int i=0;i<48;i++)
        {
            map[star1x[i]+67][star1y[i]]=1;
            map[star1x[i]+87][star1y[i]+33]=1;
        }
        for (int i=0;i<9;i++)
        {
            map[fly2x[i]][fly2y[i]-1]=1;
            map[fly6x[i]+7][fly6y[i]+42]=1;
        }
    }
}

int map2::check()
{
    bool t=0;
    for(int y=0;y<map22.heightnum;y++)
    {
        if(map22.map[map22.widthnum+94][y]==1)
            t=1;
    }
    if (t==0)
        return 0;
    else
        return 1;
}
